import requests

patterns_scores = {
    "CMMMM": 10000, "MCMMM": 10000, "MMCMM": 10000,"MMMCM": 10000,"MMMMC": 10000,
    "OOOOC": 6000,"COOOO": 6000,".CMMM.": 5000,".MCMM.": 5000,".MMCM.": 5000,".MMMC.": 5000,
    "COOO.": 2500,".OOOC": 2500,".OOCO.": 2500,".OCOO.": 2500,
    "OCMMM.": 2000,"OMCMM.": 2000,"OMMCM.": 2000, "OMMMC.": 2000,".CMMMO": 2000,".MCMMO": 2000,".MMCMO": 2000,".MMMCO": 2000,
    ".MMC.": 400, ".MCM.": 400,".CMM.": 400,".OOC": 400,"COO.": 400,"MOOOC": 400,"COOOM": 400,
    ".MMCO": 200,".MCMO": 200,".CMMO": 200,"OMMC.": 200,"OMCM.": 200,"OCMM.": 200,"MOOC": 200,"COOM": 200,
    ".MC.": 50,".CM.": 50,
    "OTHER": 20
}

def parse_board(board_str):
    size = 15  # 棋盘的尺寸是15x15
    board = [['.' for _ in range(size)] for _ in range(size)]  # 初始化一个15x15的棋盘，用'.'填充
    letters = 'ABCDEFGHIJKLMNO'  # 棋盘行对应的字母

    for i in range(0, len(board_str), 2):  # 每两个字符代表一个坐标
        row = letters.index(board_str[i].upper())  # 获取行索引
        col = letters.index(board_str[i + 1].upper())  # 获取列索引
        if i // 2 % 2 == 0:  # 偶数步为'x'
            board[row][col] = 'x'
        else:  # 奇数步为'o'
            board[row][col] = 'o'

    return board  # 返回解析后的棋盘

def convert_line(line, key_char):
    return line.replace('x', 'M').replace('o', 'O').replace('.', '.').replace(key_char, 'C')

def get_horizontal(board, row, col):
    line = []  # 初始化一个空列表存储水平线字符
    for c in range(col - 4, col + 5):  # 从col-4到col+5的范围内获取字符
        if 0 <= c < 15:  # 如果列索引在棋盘范围内
            if c == col:
                line.append('C')  # 将关键点用'C'表示
            else:
                line.append(board[row][c])  # 添加棋盘上的字符
        else:
            line.append('')  # 超出范围添加空字符
    return convert_line(''.join(line), 'C')  # 返回连接后的字符串并进行字符替换

def get_diagonal_lr(board, row, col):
    line = []  # 初始化一个空列表存储左上到右下的对角线字符
    for i in range(-4, 5):  # 从-4到+5的范围内获取字符
        r = row + i  # 计算行索引
        c = col + i  # 计算列索引
        if 0 <= r < 15 and 0 <= c < 15:  # 如果行和列索引在棋盘范围内
            if r == row and c == col:
                line.append('C')  # 将关键点用'C'表示
            else:
                line.append(board[r][c])  # 添加棋盘上的字符
        else:
            line.append('')  # 超出范围添加空字符
    return convert_line(''.join(line), 'C')  # 返回连接后的字符串并进行字符替换

def get_vertical(board, row, col):
    line = []  # 初始化一个空列表存储垂直线字符
    for r in range(row - 4, row + 5):  # 从row-4到row+5的范围内获取字符
        if 0 <= r < 15:  # 如果行索引在棋盘范围内
            if r == row:
                line.append('C')  # 将关键点用'C'表示
            else:
                line.append(board[r][col])  # 添加棋盘上的字符
        else:
            line.append('')  # 超出范围添加空字符
    return convert_line(''.join(line), 'C')  # 返回连接后的字符串并进行字符替换

def get_diagonal_rl(board, row, col):
    line = []  # 初始化一个空列表存储右上到左下的对角线字符
    for i in range(-4, 5):  # 从-4到+5的范围内获取字符
        r = row + i  # 计算行索引
        c = col - i  # 计算列索引
        if 0 <= r < 15 and 0 <= c < 15:  # 如果行和列索引在棋盘范围内
            if r == row and c == col:
                line.append('C')  # 将关键点用'C'表示
            else:
                line.append(board[r][c])  # 添加棋盘上的字符
        else:
            line.append('')  # 超出范围添加空字符
    return convert_line(''.join(line), 'C')  # 返回连接后的字符串并进行字符替换

def find_best_move(board_str):
    board = parse_board(board_str)  # 调用parse_board函数解析棋盘



    # 找到所有空白点
    letters = 'ABCDEFGHIJKLMNO'  # 棋盘行对应的字母
    empty_coords = []  # 存储空白点的坐标
    for row in range(15):
        for col in range(15):
            if board[row][col] == '.':
                empty_coords.append((letters[row], letters[col]))

    # 获取所有空白点的棋型字符串并计算分数
    scores = {}  # 存储每个空白点的分数
    for coord in empty_coords:  # 遍历所有空白点坐标
        row = letters.index(coord[0].upper())  # 获取行索引
        col = letters.index(coord[1].upper())  # 获取列索引

        patterns = [
            get_horizontal(board, row, col),  # 获取水平棋型
            get_diagonal_lr(board, row, col),  # 获取左上到右下的对角线棋型
            get_vertical(board, row, col),  # 获取垂直棋型
            get_diagonal_rl(board, row, col)  # 获取右上到左下的对角线棋型
        ]

        # 棋型字符串进行分数匹配
        max_score = 0
        #print(f"Coord: {coord}")
        for pattern in patterns:
            pattern_score = 20  # Default score for "OTHER" patterns
            for key_pattern, key_score in patterns_scores.items():
                if key_pattern in pattern:
                    pattern_score = key_score
                    break
            max_score += pattern_score
            #print(f"Pattern: {pattern}, Score: {pattern_score}")

        scores[coord] = max_score  # 存储最高分数
        #print(f"Total Score for {coord}: {max_score}\n")

    # 找到最高分数的位置
    best_move = max(scores, key=scores.get)
    best_score = scores[best_move]

    return best_move, best_score
get_html = requests .get("http://183.175.12.27:8002/step_08")
boards = get_html.json()['questions']

# 用于存储结果的列表
results = []

for board_str in boards:
    best_move, best_score = find_best_move(board_str)
    # 将坐标转换为小写字母并添加到结果列表
    best_move_str = f"{best_move[0].lower()}{best_move[1].lower()}"
    results.append(best_move_str)

# 将结果列表用逗号隔开并存储到变量ans中
ans = ",".join(results)
param = {'ans': ans}
get_html = requests .get("http://183.175.12.27:8002/step_08", params=param)
print(get_html.text)
print(ans)
